package com.app.business.web.business;

import com.app.business.model.OverAreaPrice;
import com.app.business.model.ZsProject;
import com.app.business.service.ContractService;
import com.app.business.service.OverAreaPriceService;
import com.app.business.service.ZsProjectService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 征收项目管理
 * Created by yby on 2019/2/11 14:49.
 */

@Controller
@RequestMapping("/business/zsProject/*")
@SuppressWarnings("all")
public class ZsProjectController {

    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private OverAreaPriceService overAreaPriceService;

    /**
     * 征收项目数据列表
     */
    @RequestMapping("list")
    @RequiresPermissions("zsProject:list")
    public void list() {
    }

    /**
     * 加载征收项目数据
     *
     * @return
     */
    @RequestMapping("loadZsProjectList")
    @ResponseBody
    @RequiresPermissions("zsProject:loadZsProjectList")
    public Map loadFamilyList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<ZsProject> zsProjectPageBean = this.zsProjectService.getZsProjectPageBean();
        List<ZsProject> zsProjectList = zsProjectPageBean.getDatas();
        Map<String, String> townMap = dataDictService.getMapByParentNameForValue_Name("乡镇");
        Map<String, DataDict> showMap = dataDictService.getMapByParentNameForValue("征收项目公示隐藏");
        if (!CollectionUtils.isEmpty(zsProjectList)) {
            Map<String, Object> item = null;
            for (ZsProject zsProject : zsProjectList) {
                item = Maps.newHashMap();
                item.put("id", zsProject.getId());
                item.put("town", StringUtils.getNotNull(townMap.get(zsProject.getTown())));
                item.put("projectCode", StringUtils.getNotNull(zsProject.getProjectCode()));
                item.put("projectName", StringUtils.getNotNull(zsProject.getProjectName()));
                item.put("azProjectName", StringUtils.getNotNull(zsProject.getAzProjectName()));
                item.put("startYear", DateUtils.dateToStringDefault(zsProject.getStartYear()));
                item.put("showStr", StringUtils.dataDictShow(showMap.get(zsProject.getHideStatus().toString())));
                item.put("memo", StringUtils.getNotNull(zsProject.getMemo()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(zsProjectPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 征收项目数据登记/修改
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("form")
    @RequiresPermissions("zsProject:form")
    public void form(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        if (zsProject == null) {
            zsProject = new ZsProject();
        }
        modelMap.addAttribute("zsProject", zsProject);
    }

    /**
     * 征收项目数据保存0000
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("zsProject:save")
    public synchronized Response save(HttpServletRequest request) throws AjaxException {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        if (zsProject == null) {
            zsProject = new ZsProject();
        }
        WebUtils.bind(request, zsProject);
        this.zsProjectService.save(zsProject);

        return new Response("0001", "保存成功");
    }

    /**
     * 征收项目数据配置表单
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("dataForm")
    @RequiresPermissions("zsProject:dataForm")
    public void dataForm(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "未找到征收项目！");
        List<OverAreaPrice> overAreaPriceList = this.overAreaPriceService.getOverAreaPriceListByZsProjectId(zsProject.getId());
        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("overAreaPriceList", overAreaPriceList);
    }

    /**
     * 征收项目参数配置数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("dataFormSave")
    @ResponseBody
    @RequiresPermissions("zsProject:dataFormSave")
    public synchronized Response dataFormSave(HttpServletRequest request) throws AjaxException {
        Response response = this.zsProjectService.dataFormSave(request);
        return response;
    }

    /**
     * 征收项目数据查看
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("view")
    @RequiresPermissions("zsProject:view")
    public void view(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "数据错误！");
        Map<String, String> townMap = dataDictService.getMapByParentNameForValue_Name("乡镇");
        List<OverAreaPrice> overAreaPriceList = this.overAreaPriceService.getOverAreaPriceListByZsProjectId(zsProject.getId());
        modelMap.addAttribute("zsProject", zsProject);
        modelMap.addAttribute("townMap", townMap);
        modelMap.addAttribute("overAreaPriceList", overAreaPriceList);
    }

    /**
     * 批量删除家庭数据
     *
     * @param request
     * @return
     */
    @RequestMapping("delZsProjectById")
    @ResponseBody
    @RequiresPermissions("zsProject:delZsProjectById")
    public synchronized Response delFamilyById(HttpServletRequest request) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkAjaxArguement(zsProject != null, "1111", "数据错误！");
        try {
            //先删除参数配置面积区间信息
            this.overAreaPriceService.deleteOverAreaPriceByZsProjectId(zsProject.getId());
            this.zsProjectService.delete(zsProject.getId());
        } catch (Exception e) {
            Precondition.checkAjaxArguement(false, "9999", "该项目登记征收信息，无法删除");
        }
        return new Response("0001", "操作成功");
    }


    /**
     * 验证项目编号是否已存在
     *
     * @param request
     * @return
     */
    @RequestMapping("validateProjectCode")
    @ResponseBody
    @RequiresPermissions("zsProject:validateProjectCode")
    public String validateProjectCode(HttpServletRequest request) {
        String projectCode = ParamUtils.getString(request, "projectCode", "");
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);

        String hql = " select * from zs_project where id !=:zsProjectId and project_code =:projectCode ";
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("projectCode", projectCode);
        map.put("zsProjectId", zsProjectId);

        List list = this.zsProjectService.findSqlObject(hql, map);
        String msg = "";//返回的验证信息
        if (CollectionUtils.isNotEmpty(list)) {
            msg = "项目编号已存在";
        }
        return msg;
    }

//
//    /**
//     * 按项目设置选房时间安排
//     *
//     * @return
//     */
//    @RequestMapping("batchChooseDateSet")
//    @ResponseBody
//    @RequiresPermissions("zsProject:batchChooseDateSet")
//    public synchronized Response batchChooseDateSet(HttpServletRequest request) {
//        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
//        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
//        Precondition.checkAjaxArguement(zsProject != null, "0012", "项目不存在");
//
//        this.contractService.batchSetChooseDate(zsProject);
//        return new Response("0001", "选房时间安排设置成功");
//    }

    /**
     * 征收项目协议文本核对
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("viewPrint")
    @RequiresPermissions("zsProject:viewPrint")
    public void viewPrint(HttpServletRequest request, ModelMap modelMap) {
        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
        Precondition.checkSqdsArguement(zsProject != null, "数据错误！");

        modelMap.addAttribute("zsProject", zsProject);
    }


//
//    /**
//     * 按项目批量执行计算方法
//     *
//     * @return
//     */
//    @RequestMapping("calculateAll")
//    @ResponseBody
//    @RequiresPermissions("zsProject:calculateAll")
//    public synchronized Response calculateAll(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        int zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
//        ZsProject zsProject = this.zsProjectService.findOne(zsProjectId);
//        Precondition.checkAjaxArguement(zsProject != null, "0012", "项目不存在");
//
//        this.contractService.batchSetCalculate(zsProject);
//        return new Response("0001", "计算方法批量设置成功");
//    }
}
